//
//  HMEmoticonPageCellCollectionViewCell.swift
//  04-表情键盘
//
//  Created by Admin on 16/9/16.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

/// 每页显示20个表情按钮
let HMEmoticonNumberOfPage = 20

/// 每页显示7列
let HMEmoticonColumnOfPage = 7

/// 每页显示3行
let HMEmoticonRowOfPage = 3

protocol HMEmoticonPageCellDelegate: NSObjectProtocol {
    /// 表情按钮点击的代理事件
    func emoticonPageCell(didSelectedEmoticon emoticon: HMEmoticonModel)
    
    /// 删除按钮点击的代理事件
    func emoticonPageCellDidClickDeleteButton()
}

class HMEmoticonPageCell: UICollectionViewCell {
    
    /// 代理
    weak var delegate: HMEmoticonPageCellDelegate?
    
    var indexPath: IndexPath? {
        didSet {
            messageLabel.text = "当前第\(indexPath!.section)组, 第\(indexPath!.item)页"
            
            // 不是第0组就隐藏最近使用label
            recentInfoLabel.isHidden = indexPath?.section != 0
        }
    }
    
    /// 每页要显示的emoticon表情模型
    var pageEmoticons: [HMEmoticonModel]? {
        didSet {
            // 先隐藏所有的按钮
            for button in emoticonButtons {
                button.isHidden = true
            }
            
            // 遍历传入的每个模型,获取对应的按钮,将模型赋值给表情按钮
            for (index, emoticon) in pageEmoticons!.enumerated() {
                // 获取每个模型
                // 找到对应的按钮
                let emoticonButton = emoticonButtons[index]
                emoticonButton.emoticon = emoticon
                
                emoticonButton.isHidden = false
            }
        }
    }
    
    // MARK: - 属性
    /// 20个表情按钮数组
    private lazy var emoticonButtons = [HMEmoticonButton]()

    /**
     添加20个表情按钮到cell里面
     */
    private func addEmoticonButtons() {
        for _ in 0..<HMEmoticonNumberOfPage {
            // 创建按钮
            let button = HMEmoticonButton()
//            button.backgroundColor = UIColor.random
            
            button.addTarget(self, action: #selector(didClickEmoticonButton(button:)), for: UIControlEvents.touchUpInside)
            
            // 按钮添加到contentView
            contentView.addSubview(button)
            
            // 将按钮添加到数组,方便后面遍历按钮
            emoticonButtons.append(button)
        }
    }
    
    /**
     按钮点击事件
     parameter emoticonButton: 被点击的按钮
     */
    @objc private func didClickEmoticonButton(button: HMEmoticonButton) {
//        print("点击了: \(button)")
        delegate?.emoticonPageCell(didSelectedEmoticon: button.emoticon!)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        addEmoticonButtons()
        contentView.addSubview(deleteButton)
        contentView.addSubview(messageLabel)
        contentView.addSubview(recentInfoLabel)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        layoutEmoticonButtons()
    }
    
    /// 重新布局按钮位置
    private func layoutEmoticonButtons() {
        let margin: CGFloat = 5
        let bottomMargin: CGFloat = 25
        
        // 计算表情按钮的宽度与高度
        let itemW = (frame.width - 2 * margin) / CGFloat(HMEmoticonColumnOfPage)
        let itemH = (frame.height - bottomMargin) / CGFloat(HMEmoticonRowOfPage)
        
        for i in 0..<emoticonButtons.count {
            let button = emoticonButtons[i]
            
            // 计算当前按钮是处于哪一行和哪一列
            let column = i % HMEmoticonColumnOfPage
            let row = i / HMEmoticonColumnOfPage
            
            // 计算x和y值
            let x = itemW * CGFloat(column) + margin
            let y = itemH * CGFloat(row)
            button.frame = CGRect(x: x, y: y, width: itemW, height: itemH)
        }
        
        // 设置删除按钮的位置
        let deleteX = (CGFloat(HMEmoticonColumnOfPage) - 1) * itemW + margin
        let deleteY = (CGFloat(HMEmoticonRowOfPage) - 1) * itemH
        deleteButton.frame = CGRect(x: deleteX, y: deleteY, width: itemW, height: itemH)
        
        /// 最近使用表情的 label
        recentInfoLabel.center = CGPoint(x: frame.width * 0.5, y: frame.height - recentInfoLabel.frame.height)
    }
    
    /// 删除按钮点击事件
    func didClickdeleteButton() -> Void {
        delegate?.emoticonPageCellDidClickDeleteButton()
    }
    
    // MARK: - 懒加载
    // 测试label
    private lazy var messageLabel: UILabel = UILabel(frame: CGRect(origin: CGPoint(), size: CGSize(width: 300, height: 200)))
    
    /// 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        
        button.setImage(UIImage(named: "compose_emotion_delete"), for: UIControlState.normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: UIControlState.highlighted)
        
        button.addTarget(self, action: #selector(didClickdeleteButton), for: .touchUpInside)

        return button
    }()
    
    /// 最近使用表情的 Label
    private lazy var recentInfoLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.gray
        label.font = UIFont.systemFont(ofSize: 12)
        label.text = "最近使用的表情"
        label.sizeToFit()
        
        return label
    }()
}
